import { ApiRes, Token } from "@/types/data";
import { RootThunkAction } from "@/types/store";
import { http } from "@/utils/http";
import { clearToken, setToken } from "@/utils/token";
type LoginParams = {
    mobile: string;
    code: string;
};
// 登录
export const login = (values: LoginParams): RootThunkAction => {
    return async (dispatch) => {
        const res = await http.post<ApiRes<Token>>("/authorizations", values);
        const tokens = res.data.data;
        // 设置本地token
        setToken(tokens);
        // 分发 action 将 token 保存到 redux state 中
        dispatch({ type: "login/token", payload: tokens });
    };
};
// 发送验证码
export const getCode = (mobile: string) => {
    return async () => {
        await http.get(`/sms/codes/${mobile}`);
        // 注意：验证码是发送到手机上的，因此，不需要更新Redux状态
    };
};
// 退出登录
export const logout = (): RootThunkAction => {
    return async (dispatch) => {
        dispatch({ type: "login/logout" });
        clearToken();
    };
};
